package com.example.gaoyang.config;


import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 *
 */
@Configuration
public class CaffeineConfig {

    @Bean
    public Cache<String, Boolean> caffeineCameraStatus(CacheLoader<String, Boolean> cacheLoader) {
        return Caffeine.newBuilder()
                // 设置有效期为插入后30秒
                .expireAfterWrite(30, TimeUnit.SECONDS)
                // 写入后间隔多久刷新
                .refreshAfterWrite(60,TimeUnit.SECONDS)
                .maximumSize(10_000)
                .build(cacheLoader);
    }

    /**
     * 相当于在构建LoadingCache对象的时候 build()方法中指定过期之后的加载策略方法
     * 必须要指定这个Bean，refreshAfterWrite=60s属性才生效
     */
    @Bean
    public CacheLoader<String, Boolean> cacheLoader() {
        return new CacheLoader<String, Boolean>() {
            @Override
            public Boolean load(@NonNull String key) throws Exception {
                return false;
            }
        };
    }
}
